package com.huawei.admins.platform.gateway.core.util;

import com.huawei.admins.platform.gateway.core.component.filter.GatewayFilterFactory;
import com.huawei.admins.platform.gateway.core.component.predicate.factory.RoutePredicateFactory;

import java.util.regex.Pattern;

public final class NameUtils {

    private NameUtils() {
        throw new AssertionError("Must not instantiate utility class.");
    }

    /**
     * Generated name prefix.
     */
    public static final String GENERATED_NAME_PREFIX = "_genkey_";

    private static final Pattern NAME_PATTERN = Pattern.compile("([A-Z][a-z0-9]+)");

    public static String generateName(int i) {
        return GENERATED_NAME_PREFIX + i;
    }

    public static String normalizeRoutePredicateName(Class<? extends RoutePredicateFactory> clazz) {
        return removeGarbage(clazz.getSimpleName().replace(RoutePredicateFactory.class.getSimpleName(), ""));
    }

    public static String normalizeFilterFactoryName(Class<? extends GatewayFilterFactory> clazz) {
        return removeGarbage(clazz.getSimpleName().replace(GatewayFilterFactory.class.getSimpleName(), ""));
    }

    private static String removeGarbage(String s) {
        int garbageIdx = s.indexOf("$Mockito");
        if (garbageIdx > 0) {
            return s.substring(0, garbageIdx);
        }

        return s;
    }
}
